package com.itheima.health.controller;

import com.alibaba.fastjson.JSONObject;
import com.itheima.health.dao.MobileCommonDao;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.MobileService;
import com.itheima.health.service.SetMealService;
import com.itheima.health.util.QiniuUtils;
import com.itheima.health.vo.MobileSubmitSetMealVO;
import com.itheima.health.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangmeng
 * @description 套餐控制器
 * @date 2019/9/26
 **/
@RestController
@RequestMapping("/mobile")
@Slf4j
public class MobileController {
    @Autowired
    private MobileService mobileService;
    @Autowired
    private RedisTemplate redisTemplate;

//order/findById
    /*
      <p>体检人：{{orderInfo.member}}</p>
                            <p>体检套餐：{{orderInfo.setmeal}}</p>
                            <p>体检日期：{{orderInfo.orderDate}}</p>
                            <p>预约类型：{{orderInfo.orderType}}</p>
     */
@RequestMapping("/order/findById")
public Result findById(Integer id) {
    //注意,这里是orderid
    log.info("id={}",id);
    OrderVO orderVO =  mobileService.selectOrderInfoByOrderid(id);
    return Result.success(orderVO);
}


//ocalhost:17080/mobile/validateCode/send
    // ?type=ORDER&telephone=18033727684
    @PostMapping("/validateCode/send")
    public Result sendCode(String type,String telephone) {
        mobileService.sendCode(type, telephone);
        return Result.success(null);


    }

    /*
order/submit
{"setmealId":"12","sex":"1","telephone":"18033727684","validateCode":"0001"
,"idCard":"130406199706012119","orderDate":"2022-04-03"}
{"setmealId":"13","sex":"1","name":"张泽辉","telephone":"18833727684",
"validateCode":"7635",
"idCard":"130406199706012119","orderDate":"2022-04-04"}
*/
    @PostMapping("/order/submit")
    public Result submit(@RequestBody MobileSubmitSetMealVO mobileSubmitSetMealVO) {
        if (mobileSubmitSetMealVO.getOrderDate()==null){
            return Result.error(null,"请填写注册日期");
        }
        //用户验证,验证码是否正确,是否是老用户,新用户自动注册
        mobileService.login(mobileSubmitSetMealVO);

        //判断该体检是否约满,是否重复预约,
        mobileService.verifySubmit(mobileSubmitSetMealVO);

        //进行预约,,写表
        OrderSetting orderSetting = mobileService.submit(mobileSubmitSetMealVO);
        HashMap<String,Integer> result = new HashMap<>();
        //{"date":18,"number":200,"reservations":1}
        //注意,这里是orderid
        result.put("id",orderSetting.getId());
        result.put("number",orderSetting.getNumber());
        result.put("reservations",orderSetting.getReservations());
        log.info("result={}",result);
        //throw new BusinessRuntimeException("调试中");
        return Result.success(result,"恭喜您,预约成功");
    }





    }
